﻿using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using OpSystem.common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpSystem.manage.ajax
{
    public partial class api : BasePage
    {
        private string Action
        {
            get {
                return RequestUtility.GetFormString("action");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Action)
            {
                case "checkCode":
                    IsPassCheckMenuCode();
                    break;
                case "delmenu":
                    DelMenuById();
                    break;
                case "checksystemcode":
                    CheckSystemCode();
                    break;
                case "delsys":
                    DelSystemById();
                    break;
                case "saverolemenurela":
                    SaveRroleMenuRela();
                    break;
                case "delrole":
                    DelSystemRole();
                    break;
                case "delprovince":
                    DelLocation();
                    break;
                case "delcity":
                    DelLocation();
                    break;
                case "delarea":
                    DelLocation();
                    break;
                case "getprovince":
                    GetProvinve();
                    break;
                case "getcity":
                    GetCity();
                    break;
                case "deluser":
                    DelUser();
                    break;
                case "saveuserrolerela":
                    SaveUserRoleRela();
                    break;
                default:
                    break;
            }
        }

        ///<summary>
        ///是否通过验证
        ///</summary>
        public void IsPassCheckMenuCode()
        {
            string szReturn = AjaxResult.Error("").ToString();
            string code = RequestUtility.GetString("code");
            int id = RequestUtility.GetInt("menuId", -1);
            if (id > 0)
            {
                //修改时先验证是否跟当前代码相同，如果不同则验证是否重复
                AuthorityMenuInfo menuObj = AuthorityMenuOperate.GetByID(id);
                if (menuObj != null)
                {
                    if (menuObj.MenuCode == code)
                    {
                        szReturn = AjaxResult.Success("").ToString();
                    }
                    else
                    {
                        AuthorityMenuInfo info = AuthorityMenuOperate.QuerySingle(a=>a.MenuCode==code&&a.IsActive==1);
                        if (info==null)
                        {
                            szReturn = AjaxResult.Success("").ToString();
                        }
                    }
                }
            }
            else
            {
                AuthorityMenuInfo info = AuthorityMenuOperate.QuerySingle(a => a.MenuCode == code && a.IsActive == 1);
                if (info == null)
                {
                    szReturn = AjaxResult.Success("").ToString();
                }
            }

            Response.Write(szReturn);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        public void DelMenuById()
        {
            string szReturn = AjaxResult.Error("删除失败").ToString();
            int id = RequestUtility.GetInt("id", -1);
            AuthorityMenuInfo menuObj = AuthorityMenuOperate.GetByID(id);
            if (menuObj != null)
            {
                menuObj.IsActive = 0;
                menuObj.UpdateTime = DateTime.Now;
                AuthorityMenuOperate.Update(menuObj);
                szReturn = AjaxResult.Success("删除成功").ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 验证系统编码是否重复
        /// </summary>
        public void CheckSystemCode()
        {
            string szReturn = AjaxResult.Error("").ToString();
            string code = RequestUtility.GetString("code");
            int id = RequestUtility.GetInt("id", -1);
            if (id > 0)
            {
                //修改时先验证是否跟当前代码相同，如果不同则验证是否重复
                AuthoritySystemInfo menuObj = AuthoritySystemOperate.GetByID(id);
                if (menuObj != null)
                {
                    if (menuObj.SystemCode == code)
                    {
                        szReturn = AjaxResult.Success("").ToString();
                    }
                    else
                    {
                        AuthoritySystemInfo info = AuthoritySystemOperate.QuerySingle(a => a.SystemCode == code && a.IsActive == 1);
                        if (info == null)
                        {
                            szReturn = AjaxResult.Success("").ToString();
                        }
                    }
                }
            }
            else
            {
                AuthoritySystemInfo info = AuthoritySystemOperate.QuerySingle(a => a.SystemCode == code && a.IsActive == 1);
                if (info == null)
                {
                    szReturn = AjaxResult.Success("").ToString();
                }
            }

            Response.Write(szReturn);
        }

        /// <summary>
        /// 删除系统
        /// </summary>
        private void DelSystemById()
        {
            string szReturn = AjaxResult.Error("删除失败").ToString();
            int id = RequestUtility.GetInt("id", -1);
            AuthoritySystemInfo menuObj = AuthoritySystemOperate.GetByID(id);
            if (menuObj != null)
            {
                menuObj.IsActive = 0;
                menuObj.UpdateTime = DateTime.Now;
                AuthoritySystemOperate.Update(menuObj);
                szReturn = AjaxResult.Success("删除成功").ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 存储权限配置
        /// </summary>
        private void SaveRroleMenuRela()
        {
            string szReturn = AjaxResult.Error("").ToString();
            int roleid = RequestUtility.GetInt("roleid", -1);
            string menuIDList = RequestUtility.GetFormString("menuid");
            if (roleid < 1)
            {
                szReturn = AjaxResult.Error("检测不到角色ID").ToString();
            }
            else
            {
                AuthorityRoleMenuRelaOperate.DeleteRoleMenu(roleid);
                IList<AuthorityMenuInfo> sysList = AuthorityMenuOperate.QueryList(a => a.IsActive == 1);
                // OpOperationLogOperate.SetLog(OpUserID, OpUserInfo.OpUserName, (short)LogType.OpUserRoleManage, "删除角色权限配置，roleid:" + RoleID, RoleID);
                string[] strFIDAll = menuIDList.Split(',');
                foreach (string strID in strFIDAll)
                {
                    if (strID == "") continue;
                    var obj = sysList.Where(a => a.MenuID == int.Parse(strID)).ToList();
                    if (obj.Count==0)
                    {
                        continue;
                    }
                    AuthorityRoleMenuRelaInfo info = new AuthorityRoleMenuRelaInfo
                    {
                        CreatedDate = DateTime.Now,
                        RoleID = roleid,
                        IsActive = 1,
                        MenuID = int.Parse(strID),
                        UpdateTime = DateTime.Now,
                        SystemID = obj[0].SystemID
                    };
                    AuthorityRoleMenuRelaOperate.Insert(info);
                    // OpOperationLogOperate.SetLog(null, info, OpUserID, OpUserInfo.OpUserName, (short)LogType.OpUserRoleManage, info.RelaID);
                }
                szReturn = AjaxResult.Success("").ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 删除系统角色
        /// </summary>
        private void DelSystemRole()
        {
            string szReturn = AjaxResult.Error("删除失败").ToString();
            int id = RequestUtility.GetInt("id", -1);
            AuthorityRoleInfo roleObj = AuthorityRoleOperate.GetByID(id);
            if (roleObj != null)
            {
                roleObj.IsActive = 0;
                roleObj.UpdateTime = DateTime.Now;
                AuthorityRoleOperate.Update(roleObj);
                szReturn = AjaxResult.Success("删除成功").ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 删除省
        /// </summary>
        private void DelLocation()
        {
            string szReturn = AjaxResult.Error("删除失败").ToString();
            int id = RequestUtility.GetInt("id", -1);
            BaseLocationInfo info = BaseLocationOperate.GetByID(id);
            if (info != null)
            {
                info.LocationStatus = 0;
                BaseLocationOperate.Update(info);
                szReturn = AjaxResult.Success("删除成功").ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 省
        /// </summary>
        private void GetProvinve()
        {
            string szReturn = AjaxResult.Error("").ToString();
            var dt = BaseLocationOperate.QueryList(a=>a.LocationStatus==1&&a.LocationParentID==0);
            if (dt.Count > 0)
            {
                szReturn = AjaxResult.Success(dt).ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 城市
        /// </summary>
        private void GetCity()
        {
            string szReturn = AjaxResult.Error("").ToString();
            int provinceId = RequestUtility.GetInt("provinceId", 0);
            var dt = BaseLocationOperate.QueryList(a => a.LocationStatus == 1 && a.LocationParentID == provinceId);
            if (dt.Count > 0)
            {
                szReturn = AjaxResult.Success(dt).ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        private void DelUser()
        {
            string szReturn = AjaxResult.Error("删除失败").ToString();
            int id = RequestUtility.GetInt("id", -1);
            AuthorityUserInforInfo userObj = AuthorityUserInforOperate.GetByID(id);
            if (userObj != null)
            {
                userObj.IsActive = 0;
                userObj.UpdateTime = DateTime.Now;
                AuthorityUserInforOperate.Update(userObj);
                szReturn = AjaxResult.Success("删除成功").ToString();
            }
            Response.Write(szReturn);
        }

        /// <summary>
        /// 配置用户角色
        /// </summary>
        private void SaveUserRoleRela()
        {
            string szReturn = AjaxResult.Error("").ToString();
            int userid = RequestUtility.GetInt("userid", -1);
            string roleIDList = RequestUtility.GetFormString("roleid");
            if (userid < 1)
            {
                szReturn = AjaxResult.Error("检测用户ID").ToString();
            }
            else
            {
                AuthorityUserRoleRelaOperate.DeleteUserRole(userid);
                IList<AuthorityMenuInfo> sysList = AuthorityMenuOperate.QueryList(a => a.IsActive == 1);
                // OpOperationLogOperate.SetLog(OpUserID, OpUserInfo.OpUserName, (short)LogType.OpUserRoleManage, "删除角色权限配置，roleid:" + RoleID, RoleID);
                string[] strFIDAll = roleIDList.Split(',');
                foreach (string strID in strFIDAll)
                {
                    if (strID == "") continue;
                    AuthorityUserRoleRelaInfo info = new AuthorityUserRoleRelaInfo
                    {
                        CreatedDate = DateTime.Now,
                        RoleID = int.Parse(strID),
                        IsActive = 1,
                        UserID = userid,
                        UpdatedDate = DateTime.MinValue
                    };
                    AuthorityUserRoleRelaOperate.Insert(info);
                    // OpOperationLogOperate.SetLog(null, info, OpUserID, OpUserInfo.OpUserName, (short)LogType.OpUserRoleManage, info.RelaID);
                }
                szReturn = AjaxResult.Success("").ToString();
            }
            Response.Write(szReturn);
        }
    }
}